
package com.example.gymmanagebackend.controller;

import com.example.gymmanagebackend.pojo.CourseReservation;
import com.example.gymmanagebackend.pojo.CoachReservation;
import com.example.gymmanagebackend.service.CourseReservationService;
import com.example.gymmanagebackend.service.CoachReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/reservation")
public class ReservationController {

    @Autowired
    private CourseReservationService courseReservationService;

    @Autowired
    private CoachReservationService coachReservationService;

    @PostMapping("/course")
    public String addCourseReservation(@RequestBody CourseReservation reservation) {
        int result = courseReservationService.addCourseReservation(reservation);
        return result > 0 ? "预约成功" : "预约失败";
    }

    @GetMapping("/course/{username}")
    public List<CourseReservation> getCourseReservationsByUsername(@PathVariable String username) {
        return courseReservationService.getCourseReservationsByUsername(username);
    }

    @DeleteMapping("/delete-course-reservation")
    public String deleteCourseReservationByCondition(@RequestParam String courseName, @RequestParam String reservationTime) {
        int result = courseReservationService.deleteCourseReservationByCondition(courseName, reservationTime);
        return result > 0 ? "取消成功" : "取消失败";
    }

    @PostMapping("/coach")
    public String addCoachReservation(@RequestBody CoachReservation reservation) {
        System.out.println("Received reservation: " + reservation);
        int result = coachReservationService.addCoachReservation(reservation);
        return result > 0 ? "预约成功" : "预约失败";
    }

    @GetMapping("/coach/{username}")
    public List<CoachReservation> getCoachReservationsByUsername(@PathVariable String username) {
        return coachReservationService.getCoachReservationsByUsername(username);
    }

    @DeleteMapping("/delete-coach-reservation")
    public String deleteCoachReservationByCondition(@RequestParam String coachName, @RequestParam String reservationTime) {
        int result = coachReservationService.deleteCoachReservationByCondition(coachName, reservationTime);
        return result > 0 ? "取消成功" : "取消失败";
    }

    @PostMapping("/reserve-course")
    public String reserveCourse(@RequestBody CourseReservation reservation) {
        String username = reservation.getUsername();
        String courseName = reservation.getCourseName();
        String reservationTime = reservation.getReservationTime();
        Date reservationDate = reservation.getReservationDate();

        if(courseReservationService.isCourseReservationExists(username, courseName, reservationTime, reservationDate)) {
            return "您已预约过啦";
        } else {
            int result = courseReservationService.addCourseReservation(reservation);
            return result > 0 ? "预约成功" : "预约失败";
        }
    }

    @PostMapping("/reserve-coach")
    public String reserveCoach(@RequestBody CoachReservation reservation) {
        String username = reservation.getUsername();
        String coachName = reservation.getCoachName();
        String reservationTime = reservation.getReservationTime();
        Date reservationDate = reservation.getReservationDate();

        // 检查是否已经预约了相同教练的相同时间的课程
        if (coachReservationService.isCoachReservationExists(username, coachName, reservationTime, reservationDate)) {
            return "您已预约过啦";
        } else {
            // 设置预约日期为当前日期（如果前端没有传递）
            if (reservationDate == null) {
                reservation.setReservationDate(new Date());
            }
            int result = coachReservationService.addCoachReservation(reservation);
            return result > 0 ? "预约成功" : "预约失败";
        }
    }
    @GetMapping("/all-coach-reservations")
    public List<CoachReservation> getAllCoachReservations() {
        return coachReservationService.getAllCoachReservations();
    }

    @GetMapping("/all-course-reservations")
    public List<CourseReservation> getAllCourseReservations() {
        return courseReservationService.getAllCourseReservations();
    }
}